package com.isoft.demo.service.impl;

import com.isoft.demo.common.ResponseData;
import com.isoft.demo.dao.OrderDao;
import com.isoft.demo.entity.Address;
import com.isoft.demo.entity.Customer;
import com.isoft.demo.entity.Orders;
import com.isoft.demo.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @ClassName:OrderServiceImpl
 * @Description: TODO
 * @Author guowei
 * @Date:2021/6/26 10:41
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao dao;
    @Override
    public ResponseData add(Orders orders) {
        int n = dao.add(orders);
        ResponseData rs = new ResponseData();
        List list = new ArrayList();
        if(n > 0){
            rs.setCode(0);
            rs.setMsg("订单下单成功！");
            list.add(orders);
            rs.setData(list);
        } else{
            rs.setCode(1);
            rs.setMsg("订单下单失败!");
        }
        return rs;
    }

    @Override
    public ResponseData search() {
        List<Orders> list = dao.search();
        ResponseData rs = new ResponseData();
        if(list != null){
            rs.setCode(0);
            rs.setMsg("ok");
            rs.setData(list);
        } else {
            rs.setCode(1);
            rs.setMsg("error");
        }
        return rs;
    }

    @Override
    public ResponseData searchById(int oid) {
        List<Orders> list = dao.searchById(oid);
        ResponseData rs = new ResponseData();
        if(list != null){
            rs.setCode(0);
            rs.setMsg("ok");
            rs.setData(list);
        } else {
            rs.setCode(1);
            rs.setMsg("error");
        }
        return rs;
    }

    @Override
    public ResponseData addAddress(Address address) {
        int n = dao.addAddress(address);
        ResponseData rs = new ResponseData();
        List list = new ArrayList();
        if(n > 0){
            rs.setCode(0);
            rs.setMsg("添加成功！");
            list.add(address);
            rs.setData(list);
        } else{
            rs.setCode(1);
            rs.setMsg("添加失败!");
        }
        return rs;
    }

    @Override
    public ResponseData queryByCid(int cid) {
        List<Orders> list = dao.queryByCid(cid);
        ResponseData rs = new ResponseData();
        if(list != null){
            rs.setCode(0);
            rs.setMsg("ok");
            rs.setData(list);
        } else {
            rs.setCode(1);
            rs.setMsg("error");
        }
        return rs;
    }

    @Override
    public ResponseData sumByCid(int cid) {
        List<Orders>list = dao.sumByCid(cid);
        ResponseData rs = new ResponseData();
        if(list != null){
            rs.setCode(0);
            rs.setMsg("ok");
            rs.setData(list);
        } else {
            rs.setCode(1);
            rs.setMsg("error");
        }
        return rs;
    }

    @Override
    public ResponseData searchAddress() {
        List<Address>list = dao.searchAddress();
        ResponseData rs = new ResponseData();
        if(list != null){
            rs.setCode(0);
            rs.setMsg("ok");
            rs.setData(list);
        } else {
            rs.setCode(1);
            rs.setMsg("error");
        }
        return rs;
    }


}
